const mysql = require('mysql');
const fs = require('fs');
const query = require('./utils/query')

// scp -r hc20.log  laner@10.122.251.229:/Users/laner/Desktop/log
// grep "cn.zcyun.xcloud.authcenter.controller.ControlAspect c.z.x.a.c.ControlAspect doAround START_REQUEST=url:http://10.112.73.19:2008/v1/user/regist.*password\":\"[A-Za-z]\{5\}[0-9]\{5\}" /data/logs/xcloud-authcenter/xcloud-authcenter.2019-05-31_0.info.log | awk -F '"telephone":' '{print $2}' | awk -F "," '{print $1}' > /home/znuser/hc_05_31_19.log

// grep "cn.zcyun.xcloud.authcenter.controller.ControlAspect c.z.x.a.c.ControlAspect doAround START_REQUEST=url:http://10.112.73.20:2008/v1/user/regist.*password\":\"[A-Za-z]\{5\}[0-9]\{5\}" /data/logs/xcloud-authcenter/xcloud-authcenter.2019-05-31_0.info.log | awk -F '"telephone":' '{print $2}' | awk -F "," '{print $1}' > /home/znuser/hc_05_31_20.log

// ls *.tar.gz | xargs -n1 tar xzvf

// 设置统计月份
let month = ''

let telList = fs.readFileSync(`/Users/laner/Desktop/log/hc_05_31_19.log`, 'utf8').toString().split("\n");
let tels = uniq(telList.join(','))
let out = fs.createWriteStream(`./${month}.txt`);
let outHei = fs.createWriteStream(`./${month}Hei.txt`);
let startTime = `2019-05-31T00:00:00`;
let endTime = `2019-05-31T23:59:59`;

// ./mysqldump -h 10.112.73.47 -uselect -p --no-create-db=TRUE --no-create-info=TRUE --add-drop-table=FALSE gomesmart user --where="uid in ('03c535f9a73946478163b0c9704d83aa')" > /Users/laner/Desktop/user.back.sql

let sqlNormal = "SELECT user1.uid, user1.create_time FROM user user1 where user1.uid not in " +
  "(SELECT user2.uid FROM user user2 where user2.telephone in (" + tels + ")" + "and user2.uid not in (select logininfo.uid from user_login_info logininfo)) " +
  "and user1.create_time BETWEEN '" + startTime + "' and '" + endTime + "'";
// let sqlAll = "select uid, create_time, count(*) from `user` where create_time BETWEEN '" + startTime + "' and '" + endTime + "'";

let sqlHei = "SELECT user2.uid, user2.create_time FROM user user2 where user2.telephone in (" + tels + ")" + "and user2.uid not in (select logininfo.uid from user_login_info logininfo)"

let totalByDay = {}
let totalByMonth = {
  "normal": 0
}


const transData = (data) => {
  data = JSON.stringify(data);
  data = JSON.parse(data);
  return data;
}

const getTotal = async () => {
  // let rowNormal = await query(sqlNormal);
  // rowNormal = transData(rowNormal);
  // rowNormal.forEach(item => {
  //   let date = item.create_time.split("T")[0];
  //   totalByMonth.normal += 1;
  //   if (totalByDay[date] == null) {
  //     totalByDay[date] = 1
  //   } else {
  //     totalByDay[date] = totalByDay[date] + 1
  //   }
  // });

  // let rowTotal = await query(sqlAll);
  // rowTotal = transData(rowTotal);

  let rowHei = await query(sqlHei);
  rowHei = transData(rowHei);
  console.log("rowHei", rowHei.length)
  rowHei.forEach(item => {
    let uid = item.uid
    outHei.write(`'${uid}' \n`)
  });
  console.log("done")

  // console.log(month + "月真实注册用户数: ", totalByMonth.normal);
  // out.write(month + "月真实注册用户数: " + totalByMonth.normal + '\n')
  // console.log("全月明细")
  // out.write("全月明细: \n")
  // Object.keys(totalByDay).sort().forEach(key => {
  //   console.log(key, totalByDay[key])
  //   out.write(`${key}: ${totalByDay[key]}  \n`);
  // })
}


function uniq(array){
  var temp = []; //一个新的临时数组
  for(var i = 0; i < array.length; i++){
      if(temp.indexOf(array[i]) == -1){
          temp.push(array[i]);
      }
  }
  return temp;
}

getTotal();